package com.hy.memoryassistant;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;

/**
 * A fragment representing a single Note detail screen.
 * This fragment is either contained in a {@link NoteListActivity}
 * in two-pane mode (on tablets) or a {@link NoteDetailActivity}
 * on handsets.
 */
public class NoteDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * SQL collection
     */
    private ReminderSQLCollection mCollection;

    private Reminder mItem;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public NoteDetailFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

		mCollection = ReminderSQLCollection.getInstance(getActivity());

        if (getArguments().containsKey(ARG_ITEM_ID)) {

            mItem = mCollection.getById(getArguments().getLong(ARG_ITEM_ID));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_note_detail, container, false);

        // Show the content of the reminder
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.reminder_detail_id_value)).setText(String.format("%d", mItem.ID));
            ((TextView) rootView.findViewById(R.id.reminder_detail_title_value)).setText(mItem.title);
            ((TextView) rootView.findViewById(R.id.reminder_detail_prompt_value)).setText(mItem.prompt);
            ((TextView) rootView.findViewById(R.id.reminder_detail_summary_value)).setText(mItem.summary);
            ((TextView) rootView.findViewById(R.id.reminder_detail_tags_value)).setText(mItem.tag);
            ((TextView) rootView.findViewById(R.id.reminder_detail_ext_link_value)).setText(mItem.ext_link);
            ((TextView) rootView.findViewById(R.id.reminder_detail_media_type_value)).setText(String.format("%d", mItem.media_type));

            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(mItem.date_added);
			SimpleDateFormat dateFormatter = new SimpleDateFormat(
					"yyyy/MM/dd HH:mm:ss");
			((TextView) rootView
					.findViewById(R.id.reminder_detail_date_added_value))
					.setText(dateFormatter.format(cal.getTime()));

            cal.setTimeInMillis(mItem.next_notification);
            ((TextView) rootView
                    .findViewById(R.id.reminder_detail_next_notif_value))
					.setText(dateFormatter.format(cal.getTime()));
            ((TextView) rootView.findViewById(R.id.reminder_detail_notif_count_value)).setText(String.format("%d", mItem.notification_count));

            cal.setTimeInMillis(mItem.last_modified);
			((TextView) rootView
					.findViewById(R.id.reminder_detail_last_modified_value))
					.setText(dateFormatter.format(cal.getTime()));

        }

        return rootView;
    }
}
